home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / GearTest / Shaft.java.z / Shaft.java
Encoding:
Java Source  |  2003-08-08  |  6.6 KB  |  192 lines

  1. /*
  2.  *    @(#)Shaft.java 1.9 02/04/01 15:03:16
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.lang.Math.*;
  41. import javax.media.j3d.*;
  42. import javax.vecmath.*;
  43.  
  44. public class Shaft extends javax.media.j3d.TransformGroup {
  45.     
  46.     /**
  47.      * Construct a Shaft;
  48.      * @return a new shaft that with the specified radius centered about
  49.      * the origin an laying in the XY plane and of a specified length
  50.      * extending in the Z dimension
  51.      * @param radius radius of shaft
  52.      * @param length shaft length shaft extends from -length/2 to length/2 in
  53.      * the Z dimension
  54.      * @param segmentCount number of segments for the shaft face
  55.      * @param look the Appearance to associate with this shaft
  56.      */
  57.     public Shaft(float radius, float length, int segmentCount,
  58.          Appearance look) {
  59.     // The direction of the ray from the shaft's center
  60.     float xDirection, yDirection;
  61.     float xShaft, yShaft;
  62.  
  63.     // The z coordinates for the shaft's faces (never change)
  64.     float frontZ = -0.5f * length;
  65.     float rearZ = 0.5f * length;
  66.  
  67.     int shaftFaceVertexCount;      // #(vertices) per shaft face
  68.     int shaftFaceTotalVertexCount;      // total #(vertices) in all teeth
  69.     int shaftFaceStripCount[] = new int[1]; // per shaft vertex count
  70.     int shaftVertexCount;      // #(vertices) for shaft
  71.     int shaftStripCount[] = new int[1]; // #(vertices) in strip/strip
  72.  
  73.     // Front and rear facing normals for the shaft's faces
  74.     Vector3f frontNormal = new Vector3f(0.0f, 0.0f, -1.0f);
  75.     Vector3f rearNormal = new Vector3f(0.0f, 0.0f, 1.0f);
  76.     // Outward facing normal
  77.     Vector3f outNormal = new Vector3f(1.0f, 0.0f, 0.0f);
  78.  
  79.     // Temporary variables for storing coordinates and vectors 
  80.     Point3f coordinate = new Point3f(0.0f, 0.0f, 0.0f);
  81.     Shape3D newShape;
  82.     
  83.     // The angle subtended by a single segment
  84.     double segmentAngle = 2.0 * Math.PI/segmentCount;
  85.     double tempAngle;
  86.  
  87.     // Allow this object to spin. etc.
  88.     this.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  89.  
  90.     /* for the forward facing fan:
  91.      *               ___3___
  92.      *             -    |    -
  93.      *          /       |       \
  94.      *       4/\        |        /\2
  95.      *     /     \      |      /     \
  96.      *   /         \    |    /         \
  97.      *  :            \  |  /            :
  98.      * |--------------- *----------------|
  99.      * 5                0                1
  100.      *
  101.      * for backward facing fan exchange 1 with 5; 2 with 4, etc.
  102.      */
  103.  
  104.     // Construct the shaft's front and rear face
  105.     shaftFaceVertexCount = segmentCount + 2;
  106.     shaftFaceStripCount[0] = shaftFaceVertexCount;
  107.  
  108.     TriangleFanArray frontShaftFace
  109.         = new TriangleFanArray(shaftFaceVertexCount,
  110.                      GeometryArray.COORDINATES
  111.                      | GeometryArray.NORMALS,
  112.                      shaftFaceStripCount);
  113.  
  114.     TriangleFanArray rearShaftFace
  115.         = new TriangleFanArray(shaftFaceVertexCount,
  116.                      GeometryArray.COORDINATES
  117.                      | GeometryArray.NORMALS,
  118.                      shaftFaceStripCount);
  119.  
  120.     coordinate.set(0.0f, 0.0f, frontZ);
  121.     frontShaftFace.setCoordinate(0, coordinate);
  122.     frontShaftFace.setNormal(0, frontNormal);
  123.  
  124.     coordinate.set(0.0f, 0.0f, rearZ);
  125.     rearShaftFace.setCoordinate(0, coordinate);
  126.     rearShaftFace.setNormal(0, rearNormal);
  127.  
  128.     for(int index = 1; index < segmentCount+2; index++) {
  129.  
  130.         tempAngle = segmentAngle * -(double)index;
  131.         coordinate.set(radius * (float)Math.cos(tempAngle),
  132.                radius * (float)Math.sin(tempAngle),
  133.                frontZ);
  134.         frontShaftFace.setCoordinate(index, coordinate);
  135.         frontShaftFace.setNormal(index, frontNormal);
  136.  
  137.         tempAngle = -tempAngle;
  138.         coordinate.set(radius * (float)Math.cos(tempAngle),
  139.                radius * (float)Math.sin(tempAngle),
  140.                rearZ);
  141.         rearShaftFace.setCoordinate(index, coordinate);
  142.         rearShaftFace.setNormal(index, rearNormal);
  143.     }
  144.     newShape = new Shape3D(frontShaftFace, look);
  145.     this.addChild(newShape);
  146.     newShape = new Shape3D(rearShaftFace, look);
  147.     this.addChild(newShape);
  148.  
  149.     // Construct shaft's outer skin (the cylinder body)
  150.     shaftVertexCount = 2 * segmentCount + 2;
  151.     shaftStripCount[0] = shaftVertexCount;
  152.  
  153.     TriangleStripArray shaft
  154.         = new TriangleStripArray(shaftVertexCount,
  155.                      GeometryArray.COORDINATES
  156.                      | GeometryArray.NORMALS,
  157.                      shaftStripCount);
  158.  
  159.     outNormal.set(1.0f, 0.0f, 0.0f);
  160.  
  161.     coordinate.set(radius, 0.0f, rearZ);
  162.     shaft.setCoordinate(0, coordinate);
  163.     shaft.setNormal(0, outNormal);
  164.  
  165.     coordinate.set(radius, 0.0f, frontZ);
  166.     shaft.setCoordinate(1, coordinate);
  167.     shaft.setNormal(1, outNormal);
  168.  
  169.     for(int count = 0; count < segmentCount; count++) {
  170.         int index = 2 + count * 2;
  171.  
  172.         tempAngle = segmentAngle * (double)(count + 1);
  173.         xDirection = (float)Math.cos(tempAngle);
  174.         yDirection = (float)Math.sin(tempAngle);
  175.         xShaft = radius * xDirection;
  176.         yShaft = radius * yDirection;
  177.         outNormal.set(xDirection, yDirection, 0.0f);
  178.  
  179.         coordinate.set(xShaft, yShaft, rearZ);
  180.         shaft.setCoordinate(index, coordinate);
  181.         shaft.setNormal(index, outNormal);
  182.         
  183.         coordinate.set(xShaft, yShaft, frontZ);
  184.         shaft.setCoordinate(index + 1, coordinate);
  185.         shaft.setNormal(index + 1, outNormal);
  186.     }
  187.     newShape = new Shape3D(shaft, look);
  188.     this.addChild(newShape);
  189.     }
  190. }
  191.  
  192.